import 'package:flutter/material.dart';
import 'package:motion_toast/motion_toast.dart';

class UiUtils {
  static Future<bool> showConfirmDialog(
      {required BuildContext context, String? title, required String content}) async {
    // 显示确认对话框
    bool result = await showDialog<bool>(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text(title ?? '提示'),
              content: Text(content),
              actions: <Widget>[
                TextButton(
                  child: const Text('取消'),
                  onPressed: () {
                    Navigator.of(context).pop(false); // 返回false
                  },
                ),
                TextButton(
                  child: const Text('確認'),
                  onPressed: () {
                    Navigator.of(context).pop(true); // 返回true
                  },
                ),
              ],
            );
          },
        ) ??
        false;
    return result;
  }

  static showAlertDialog(
      {required BuildContext context, String? title, required String content}) async {
    await showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title ?? '提示'),
          content: Text(content),
          actions: <Widget>[
            TextButton(
              child: const Text('確認'),
              onPressed: () {
                Navigator.of(context).pop(true); // 返回true
              },
            ),
          ],
        );
      },
    );
  }

  static warnToast(
      {required BuildContext context,
      required String content,
      double width = 200,
      double height = 40}) {
    MotionToast.warning(
      displaySideBar: false,
      description: Text(content),
      animationDuration: const Duration(milliseconds: 100),
      toastDuration: const Duration(milliseconds: 1500),
      height: height,
      width: width,
      borderRadius: 8,
      iconSize: 24,
      position: MotionToastPosition.top,
    ).show(context);
  }
}
